/*
	Dispatch.h

	Contains the header for the Dispatch object.
	This is responsible for handling the scheduling of tasks (entity 
	actions, the main function and events, mainly) to threads.

	Author: Aidan Goddard 8/8/14
*/

#ifndef DISPATCH_H_INCLUDED
#define DISPATCH_H_INCLUDED

#include<stdint.h>
#include"SnowstormXEntity.h"

namespace Snowstorm
{
	class Dispatch
	{
		// functions
	public:
		static void WorkerDispatchTask(int thread_ID);

		static void InitDispatch(int total_threads, Entity *main_entity);

		static void SignalInputEventDispatch();
		static void SignalGameCodeDispatch();

		static void InsertNewPersistentEntity(uint32_t thread_id, Entity *entity);
		static void RemovePersistentEntity(uint32_t thread_id, Entity *entity);
		static void UpdatePersistentEntities();

	private:
		static void ExecuteTaskFrame(Entity *entity);

		// data
	public:
		// dispatch control
		static uint32_t total_threads;

	private:
		// dispatch control
		static volatile uint32_t dispatch_mode;
		static volatile uint32_t *dispatch_states;
		
		// keyboard input event processing
		static uint32_t *input_key_task_list_base;
		static uint32_t *input_key_task_list_limit;

		// main entity stuff
		static Entity *main_entity;

		// persistent entity stuff
		static Entity **persistent_entity_command_queue;
		static uint32_t persistent_entity_command_queue_length;
		static uint32_t persistent_entity_command_queue_ptr[64];

		static Entity ***persistent_entity_creation_queues;
		static uint32_t *persistent_entity_creation_counts;
		static Entity ***persistent_entity_deletion_queues;
		static uint32_t *persistent_entity_deletion_counts;
	};
};


#endif